import React, { Component } from "react";
import "./TodoMain.css";

export default class TodoMain extends Component {
    render() {
        return (
            <ul className="todo-main">
                {
                    this.props.todos.map(item => {
                        return <li key={item.id}>
                                    <label>
                                        <input type="checkbox" checked={item.done} onChange={this.change(item.id)} />
                                        <span>{item.title}</span>
                                    </label>
                                    <button
                                        className="btn btn-danger"
                                        style={{ display: item.done ? 'block' : 'none' }}
                                        onClick={this.removeTodo(item.id)}
                                    >
                                        删除
                                    </button>
                                </li>
                    })
                }
                {
                    this.props.todos.length === 0 && <li className="empty">
                        暂无数据
                    </li>
                }
            </ul>
        );
    }

    //修改状态
    change = (id) => {
        return (e) => {
            //调用父级传入的 checkTodo 方法, 修改任务的完成状态
            this.props.checkTodo(id, e.target.checked);
        }
    }

    //移除任务
    removeTodo = (id) => {
        return () => {
            //移除
            this.props.remove(id);
        }
        
       
    }
}
